﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using PlanningGIS.ArcGIS;
using PlanningGIS.Util.Log;

namespace PlanningGIS.Symbol
{
    /// <summary>
    /// 作用：单一符号
    /// </summary>
    public partial class SimpleRender : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public SimpleRender()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 符号
        /// </summary>
        private ISymbol _SimpleSymbol = null;

        /// <summary>
        /// 要渲染的图层
        /// </summary>
        private IFeatureLayer _FeatureLayer;

        /// <summary>
        /// 获取或设置要渲染的图层
        /// </summary>
        public IFeatureLayer FeatureLayer
        {
            set
            {
                _FeatureLayer = value;
                if (_FeatureLayer == null || !(_FeatureLayer is IGeoFeatureLayer)) return;
                try
                {
                    IFeatureRenderer renderer = (FeatureLayer as IGeoFeatureLayer).Renderer;
                    if (renderer == null || !(renderer is ISimpleRenderer)) return;
                    ISimpleRenderer isRenderer = (ISimpleRenderer)renderer;
                    this._SimpleSymbol = isRenderer.Symbol;
                    this.txtDescription.Text = isRenderer.Description;
                    this.txtLabel.Text = isRenderer.Label;
                    this.picSimSymbol.Image = SymbolHelper.DrawSymbol(_SimpleSymbol, this.picSimSymbol.Width, this.picSimSymbol.Height);

                }
                catch (Exception ex)
                {
                    LogHelper.WriteErrorText(ex);
                }
            }
            get
            {
                return _FeatureLayer;
            }
        }

        /// <summary>
        /// 设置后的符号
        /// </summary>
        public ISimpleRenderer SimpleRenderer
        {
            get
            {
                ISimpleRenderer isRenderer = new SimpleRendererClass();
                isRenderer.Symbol = this._SimpleSymbol;
                isRenderer.Label = this.txtLabel.Text.ToString().Trim();
                isRenderer.Description = this.txtDescription.Text.ToString().Trim();
                return isRenderer;
            }
        }

        /// <summary>
        /// 选择符号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void picSimSymbol_Click(object sender, EventArgs e)
        {
            FrmVectorStyle.ShowStyle(_SimpleSymbol,this.ParentForm, new SymbolChanged((obj, sym) =>
            {
                if (sym != null && sym.NewSymbol != null)
                {
                    _SimpleSymbol = sym.NewSymbol;
                    picSimSymbol.Image = SymbolHelper.DrawSymbol(_SimpleSymbol, this.picSimSymbol.Width, this.picSimSymbol.Height);
                }
            }));
            
        }
        
    }
}
